package com.card.gift;

import org.jboss.netty.channel.ChannelHandlerContext;

import com.card.common.MessageComm;
import com.card.game.room.AbstractRoom;
import com.card.message.JsonMessageCenter;
import com.card.storage.Item;
import com.card.user.User;
import com.card.user.UserCurrency.CurrencyKind;
import com.card.user.UserManager;
import com.card.user.UserUtils;
import com.xiaoao.net.json.IMsgProcesser;
import com.xiaoao.net.json.Router;
import com.xiaoao.net.json.message.JsonMessage;

public class GiftLogic implements IMsgProcesser{
	private static final GiftLogic instance = new GiftLogic();
    public static GiftLogic getInstance() {
        return instance;
    }
	@Override
	public void registMsgId() {
		Router.getInstance().registMsg(MessageComm.C2S_GiftBuy, this);
		Router.getInstance().registMsg(MessageComm.C2S_GiftGive, this);
	}

	@Override
	public void doProcess(int msgId, JsonMessage serverMsg, ChannelHandlerContext ctx) {
		switch (msgId) {
			case MessageComm.C2S_GiftBuy:
				buyGift(serverMsg, ctx);
				break;
			case MessageComm.C2S_GiftGive:
				giveGift(serverMsg, ctx);
				break;
		}
	}
	
	public void buyGift(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		int id = serverMsg.getAsInt("id");
		User user = (User) ctx.getAttachment();//玩家信息
		Gift gift = GiftHelp.getInstance().getGift(id);
		if(gift == null) {
			return;
		}
//		if(!user.userCurrency().spend(CurrencyKind.Diamonds, gift.getPrice())) {
//			JsonMessageCenter.getInstance().sendEorrorMessage(user, "房卡不足!");
//			return;
//		}
		//添加
//		user.userStorage().addItem(id);
//		user.sendUserUpdateMessage();
//		JsonMessage message = JsonMessage.createMessage(MessageComm.S2C_GiftBuy);
//		message.addProperty("itemId", id);
//		JsonMessageCenter.getInstance().sendMessage(message, ctx);
	}
	
//	private void giveGift(JsonMessage msg, ChannelHandlerContext ctx) {
//		User user = (User) ctx.getAttachment();//玩家信息
//		int id = msg.getAsInt("gid");
//		String friendId = msg.getAsString("fid");
//		//广播赠送特效
//		JsonMessage message = new JsonMessage(MessageComm.S2C_GiftGive);
//		message.addProperty("sendId", user.getPrimaryKey());
//		message.addProperty("receiveId", friendId);
//		message.addProperty("gift", id);
//	}
	
	private void giveGift(JsonMessage msg, ChannelHandlerContext ctx) {
		User user = (User) ctx.getAttachment();//玩家信息
		int id = msg.getAsInt("gid");
		String friendId = msg.getAsString("fid");
//		Gift gift = GiftHelp.getInstance().getGift(id);
//		if(gift == null) {
//			return;
//		}
//		User friend = UserUtils.getUserByPrimaryKey(friendId);
//		if(friend == null) {
//			JsonMessageCenter.getInstance().sendEorrorMessage(user, "好友不在线!");
//			return;
//		}
//		if(!user.userStorage().spendItem(new Item(gift.getId(), 1))) {
//			if(!user.userCurrency().spend(CurrencyKind.Diamonds, gift.getPrice())) {
//				JsonMessageCenter.getInstance().sendEorrorMessage(user, "房卡不足!");
//				return;
//			}
////			JsonMessageCenter.getInstance().sendEorrorMessage(user, "礼物不足!");
////			return;
//		}
////		friend.addCharmValue(gift.getCharmValue());
//		user.sendUserUpdateMessage();
//		friend.sendUserUpdateMessage();
		//广播赠送特效
		JsonMessage message = new JsonMessage(MessageComm.S2C_GiftGive);
		message.addProperty("sendId", user.getPrimaryKey());
		message.addProperty("receiveId", friendId);
		message.addProperty("gift", id);
		AbstractRoom room = user.getRoom();
		if(room != null) {
			room.broadcast(message);
		}
	}
}
